package 笔试真题;

import java.util.Arrays;
import java.util.Scanner;

public class Tengxun25 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = scanner.nextInt();
        }
        int ans = n-1;
        for (int len = 3; len <= n; len++) {
            int min  = Math.min(values[0], values[1]);
            int min2  = min == values[0]? values[1] :values[0];
            int min3  = 1<<30;
            for (int i = 2; i < len-1; i++) {
                if(values[i]>min3) continue;
                if(values[i]<=min){
                    min2 = min;
                    min = values[i];
                }else if(values[i]<=min2){
                    min2 = values[i];
                }else {
                    min3 = values[i];
                }
            }
            for (int i = len-1; i < n; i++) {
                if(values[i]>min3) continue;
                if(values[i]<=min){
                    min2 = min;
                    min = values[i];
                    if(values[i-len+1]==min||values[i-len+1]==min2){
                        ans ++;
                    }
                }else if(values[i]<=min2){
                    min2 = values[i];
                    if(values[i-len+1]==min||values[i-len+1]==min2){
                        ans ++;
                    }
                }
            }
        }
        System.out.println(ans);
    }
}
